﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

namespace eDoc.Controls.Menu
{
    public partial class ucMenu : System.Web.UI.UserControl
    {
        private BO.Sescurity _security = new BO.Sescurity();
        private string cssSelect="select";
        private string cssCurrent ="current";
        private string cssULShow = "";
        private string urlCurent = "";
        private string cssLi_sub_show = "";
        private string strNav = "<div class=\"nav-divider\">&nbsp;</div>";
        private string strFormatUL = "<ul class='{3}' id='ulMenu_{0}'>"+
                                        "<li><a href='{5}'><b>{1}</b></a>"+
                                            "<div class='select_sub {4}'>"+
                                                "<ul class='sub'>"+
                                                    "{2}"+
                                                "</ul>"+
                                            "</div>"+
                                    "</ul>";
        private string strFormatLI = "<li id='liMenu_{0}_{1}' class='{4}'><a href='{2}'>{3}</a></li>";
                                        

    //<ul class="current">
    //                    <li><a href="#nogo"><b>Application</b></a>                            
    //                        <div class="select_sub show">
    //                            <ul class="sub">
    //                                <li id="liViewApp" class="sub_show"><a href="/">View all application</a></li>
    //                                <li id="liAddApp"><a href="/upload.aspx">Add application</a></li>
    //                                <li><a href="#nogo" style="display:none">Edit application</a></li>
    //                            </ul>
    //                        </div>
                           
    //                    </li>
    //                </ul>

        protected void Page_Load(object sender, EventArgs e)
        {
            urlCurent = HttpContext.Current.Request.Url.AbsolutePath.ToLower().Trim() ;
            if (!IsPostBack)
            {
                if (_security.CurrentUser(HttpContext.Current).Username.ToLower() == BO.Sescurity.UserRoleAdmin)
                {
                    BindData();
                }
                else
                {
                    BindMenu();
                }

                //string menu = BO.Cached.GetCache<string>(BO.Cached.CacheName.Cache_Select_User_Menu(user));
            }
        }


        private void BindMenu()
        {
            string username = _security.CurrentUser(HttpContext.Current).Username;
            DataTable dtMenu = new DataTable();
            DataTable dtParent = new DataTable();
            DataTable dtChild = new DataTable();
            string valueParent = "";
            string valueChild = "";
            string url = "";
            string idParentChild = "-1";
            string[] strMenuID = new string[]{};
            dtParent = BO.Data.Select_Menu_by_User(username);
            if (dtParent != null && dtParent.Rows.Count > 0)
            {
                dtMenu = BO.Data.Select_User_Menu(username);
                if (dtMenu != null && dtMenu.Rows.Count > 0)
                {
                    strMenuID = dtMenu.Rows[0]["Menu_ID"].ToString().Split(",".ToCharArray());
                }
                
                string urlUL = "";
                string idParent;
                for (int i = 0; i < dtParent.Rows.Count; i++)
                {
                    valueChild = "";
                    idParent = dtParent.Rows[i]["ID"].ToString();
                    if (dtParent.Rows[i]["url"].ToString() != "")
                        urlUL = dtParent.Rows[i]["url"].ToString();
                    else
                        urlUL = "";
                    dtChild = BO.Data.Select_Menu(Convert.ToInt32(idParent));
                    if (dtChild != null && dtChild.Rows.Count > 0)
                    {
                        string childMenuID = "0";
                        for (int j = 0; j < dtChild.Rows.Count; j++)
                        {
                            childMenuID = dtChild.Rows[j]["ID"].ToString();
                            if (BO.Common.CheckMenuID(strMenuID, childMenuID))
                            {
                                url = dtChild.Rows[j]["url"].ToString().Trim().ToLower();

                                if (url == urlCurent)
                                {
                                    cssCurrent = "current";
                                    cssULShow = "show";
                                    idParentChild = dtChild.Rows[j]["ParentID"].ToString();
                                    cssLi_sub_show = "sub_show";
                                }
                                else
                                    cssLi_sub_show = "";

                                valueChild += String.Format(strFormatLI, idParent, dtChild.Rows[j]["ID"].ToString(), dtChild.Rows[j]["Url"].ToString(), dtChild.Rows[j]["Name"].ToString(), cssLi_sub_show);
                            }
                        }
                    }

                    if (i < dtParent.Rows.Count - 1)
                    {
                        if (idParentChild == idParent)
                        {
                            valueParent += String.Format(strFormatUL, idParent, dtParent.Rows[i]["Name"].ToString(), valueChild, cssCurrent, cssULShow, urlUL) + strNav;
                        }
                        else
                        {
                            valueParent += String.Format(strFormatUL, idParent, dtParent.Rows[i]["Name"].ToString(), valueChild, cssSelect, "", urlUL) + strNav;
                        }

                    }
                    else
                    {
                        if (idParentChild == idParent)
                        {
                            valueParent += String.Format(strFormatUL, idParent, dtParent.Rows[i]["Name"].ToString(), valueChild, cssCurrent, cssULShow, urlUL);
                        }
                        else
                        {
                            valueParent += String.Format(strFormatUL, idParent, dtParent.Rows[i]["Name"].ToString(), valueChild, cssSelect, "", urlUL);
                        }
                    }

                }
            }
            ltrMenu.Text = valueParent;
        }

        

        private void BindData()
        {
            DataTable dtParent = new DataTable();
            DataTable dtChild = new DataTable();
            string valueParent = "";
            string valueChild = "";
            string url = "";
            string idParentChild="-1";
            dtParent = BO.Data.Select_Menu(0);
            if (dtParent != null && dtParent.Rows.Count > 0)
            {
                string urlUL = "#";
                string idParent;
                for (int i = 0; i < dtParent.Rows.Count; i++)
                {
                    valueChild = "";
                    idParent = dtParent.Rows[i]["ID"].ToString();
                    if (dtParent.Rows[i]["url"].ToString() != "")
                        urlUL = dtParent.Rows[i]["url"].ToString();
                    else
                        urlUL = "#";
                    dtChild = BO.Data.Select_Menu(Convert.ToInt32(idParent));
                    if (dtChild != null && dtChild.Rows.Count > 0)
                    {
                        
                        for (int j = 0; j < dtChild.Rows.Count; j++)
                        {                            
                            url = dtChild.Rows[j]["url"].ToString().Trim().ToLower();

                            if (url == urlCurent)
                            {
                                cssCurrent = "current";
                                cssULShow = "show";
                                idParentChild = dtChild.Rows[j]["ParentID"].ToString();
                                cssLi_sub_show = "sub_show";
                            }
                            else
                                cssLi_sub_show = "";

                            valueChild += String.Format(strFormatLI, idParent, dtChild.Rows[j]["ID"].ToString(), dtChild.Rows[j]["Url"].ToString(), dtChild.Rows[j]["Name"].ToString(), cssLi_sub_show);
                        }                     

                        
                    }
                                        

                    if (i < dtParent.Rows.Count - 1)
                    {
                        if (idParentChild == idParent)
                        {
                            valueParent += String.Format(strFormatUL, idParent, dtParent.Rows[i]["Name"].ToString(), valueChild, cssCurrent, cssULShow, urlUL) + strNav;
                        }
                        else
                        {
                            valueParent += String.Format(strFormatUL, idParent, dtParent.Rows[i]["Name"].ToString(), valueChild, cssSelect, "", urlUL) + strNav;
                        }

                    }
                    else
                    {
                        if (idParentChild == idParent)
                        {
                            valueParent += String.Format(strFormatUL, idParent, dtParent.Rows[i]["Name"].ToString(), valueChild, cssCurrent, cssULShow, urlUL);
                        }
                        else
                        {
                            valueParent += String.Format(strFormatUL, idParent, dtParent.Rows[i]["Name"].ToString(), valueChild, cssSelect, "", urlUL);
                        }
                    }
                }
            }

            ltrMenu.Text = valueParent;
        }
    }
}